package rexsee.natives;

import rexsee.core.browser.Browser;
import rexsee.core.style.StyleSheet;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class StringAdapter extends ArrayAdapter<String> {

	private final Browser mBrowser;
	private final Context mContext;
	private final StyleSheet mStyle;
	private final String[] mOptions;

	public StringAdapter(Browser browser, StyleSheet viewStyle, String[] options) {
		super(browser.getContext(), android.R.layout.simple_spinner_item, options);
		mBrowser = browser;
		mContext = browser.getContext();
		mStyle = viewStyle;
		mOptions = options;
	}

	@Override
	public View getDropDownView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
		}
		TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
		ViewStyle.setTextViewStyle(mBrowser, tv, mStyle);
		tv.setPadding(20, 20, 20, 20);
		tv.setText(mOptions[position]);
		return convertView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(android.R.layout.simple_spinner_item, parent, false);
		}
		TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
		ViewStyle.setTextViewStyle(mBrowser, tv, mStyle);
		if (!mStyle.label_type.trim().equals("")) {
			if (mStyle.label_type.equalsIgnoreCase("html")) {
				tv.setText(Html.fromHtml(mOptions[position]));
			} else if (mStyle.label_type.equalsIgnoreCase("string")) {
				tv.setText(Html.fromHtml(mOptions[position]).toString());
			} else {
				tv.setText(mOptions[position]);
			}
		}
		return convertView;
	}
}
